package thread1;

import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

class MyTask implements Runnable {
    @Override
    public void run() {
        System.out.println("Hello, world!");
        System.out.println(Thread.currentThread().getName());
    }
}

public class DirectExecutor implements Executor {
    @Override
    public void execute(Runnable command) {
        command.run(); // 直接在调用线程中执行任务
    }

    public static void main(String[] args) {
//        Executor executor = new DirectExecutor();
//        executor.execute(() -> System.out.println("Task executed in thread: " + Thread.currentThread().getName()));

        MyTask task = new MyTask();
        ExecutorService executor2 = Executors.newSingleThreadExecutor(); // 创建一个单线程的 Executor
        executor2.execute(task); // 将任务提交给 Executor 执行
        executor2.shutdown(); // 关闭 Executor
    }
}